package com.question;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*有四个线程1、2、3、4。线程1的功能就是输出A，线程2的功能就是输出B，以此类推......... 现在有四个文件file1,file2,file3, file4。初始都为空。
        现要让四个文件呈如下格式：
        file1：A B C D A B....
        file2：B C D A B C....
        file3：C D A B C D....
        file4：D A B C D A..*/
public class Test1 {

    public  static  void  main(String []args)
    {
        final DataAndProcess dataAndProcess=new DataAndProcess();

        ExecutorService service= Executors.newCachedThreadPool();
        service.execute(new Runnable() {
            public void run() {
                for (int i=0;i<1000;i++)
                {
                    dataAndProcess.printA(i);
                }
            }
        });
        service.execute(new Runnable() {
            public void run() {
                for (int i=0;i<1000;i++)
                {
                    dataAndProcess.printB(i);

                }
            }
        });
        service.execute(new Runnable() {
            public void run() {
                for (int i=0;i<1000;i++)
                {
                    dataAndProcess.printC(i);

                }
            }
        });

        service.execute(new Runnable() {
            public void run() {
                for (int i=0;i<1000;i++)
                {
                    dataAndProcess.printD(i);

                }
            }
        });


    }
}
class DataAndProcess{
    int flag=0;
    Lock lock=new ReentrantLock();
    Condition condition1=lock.newCondition();
    Condition condition2=lock.newCondition();
    Condition condition3=lock.newCondition();
    Condition condition4=lock.newCondition();

    public void  printA(int num)
    {
        lock.lock();
         //没有到1执行
        if (flag!=0)
        {
            try {
                condition1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int temp=0;
        switch (num%4)
        {
            case  0:
                temp=1;
            break;
            case  1:
                temp=4;
                break;
            case  2:
                temp=3;
                break;
            case  3:
                temp=2;
                break;

        }
        System.out.println(Thread.currentThread().getName()+"打印输出A"+"文件"+temp);
        flag=1;
        condition2.signal();
        lock.unlock();

    }
    public void  printB(int num)
    {
        lock.lock();
        //没有到1执行
        if (flag!=1)
        {
            try {
                condition2.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int temp=0;
        switch (num%4)
        {
            case  0:
                temp=2;
                break;
            case  1:
                temp=1;
                break;
            case  2:
                temp=4;
                break;
            case  3:
                temp=3;
                break;

        }
        System.out.println(Thread.currentThread().getName()+"打印输出B"+"文件"+temp);
        flag=2;
        condition3.signal();
        lock.unlock();
    }
    public void  printC(int num)
    {
        lock.lock();
        //没有到1执行
        if (flag!=2)
        {
            try {
                condition3.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int temp=0;
        switch (num%4)
        {
            case  0:
                temp=3;
                break;
            case  1:
                temp=2;
                break;
            case  2:
                temp=1;
                break;
            case 3:
                temp=4;
                break;

        }
        System.out.println(Thread.currentThread().getName()+"打印输出C"+"文件"+temp);
        flag=3;
        condition4.signal();
        lock.unlock();
    }
    public void  printD(int num)
    {
        lock.lock();
        //没有到1执行
        if (flag!=3)
        {
            try {
                condition4.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int temp=0;
        switch (num%4)
        {
            case  0:
                temp=4;
                break;
            case  1:
                temp=3;
                break;
            case  2:
                temp=2;
                break;
            case  3:
                temp=1;
                break;

        }
        System.out.println(Thread.currentThread().getName()+"打印输出D"+"文件"+temp);
        flag=0;
        condition1.signal();
        lock.unlock();
    }

}
